package com.ruoyi.web.controller.association;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.association.domain.SysAssociation;
import com.ruoyi.association.domain.SysAssociationMember;
import com.ruoyi.association.service.ISysAssociationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 协会Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/association/association")
public class SysAssociationController extends BaseController
{
    @Autowired
    private ISysAssociationService sysAssociationService;

    /**
     * 查询协会列表
     */
    @PreAuthorize("@ss.hasPermi('association:association:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAssociation sysAssociation)
    {
        startPage();
        List<SysAssociation> list = sysAssociationService.selectSysAssociationList(sysAssociation);
        return getDataTable(list);
    }

    /**
     * 导出协会列表
     */
    @PreAuthorize("@ss.hasPermi('association:association:export')")
    @Log(title = "协会", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAssociation sysAssociation)
    {
        List<SysAssociation> list = sysAssociationService.selectSysAssociationList(sysAssociation);
        ExcelUtil<SysAssociation> util = new ExcelUtil<SysAssociation>(SysAssociation.class);
        util.exportExcel(response, list, "协会数据");
    }

    /**
     * 获取协会详细信息
     */
    @PreAuthorize("@ss.hasPermi('association:association:query')")
    @GetMapping(value = "/{associationId}")
    public AjaxResult getInfo(@PathVariable("associationId") Long associationId)
    {
        return success(sysAssociationService.selectSysAssociationByAssociationId(associationId));
    }

    /**
     * 新增协会
     */
    @PreAuthorize("@ss.hasPermi('association:association:add')")
    @Log(title = "协会", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAssociation sysAssociation)
    {
        if ("1".equals(sysAssociationService.checkAssociationCodeUnique(sysAssociation)))
        {
            return error("新增协会'" + sysAssociation.getAssociationName() + "'失败，协会编码已存在");
        }
        sysAssociation.setCreateBy(getUsername());
        return toAjax(sysAssociationService.insertSysAssociation(sysAssociation));
    }

    /**
     * 修改协会
     */
    @PreAuthorize("@ss.hasPermi('association:association:edit')")
    @Log(title = "协会", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAssociation sysAssociation)
    {
        if ("1".equals(sysAssociationService.checkAssociationCodeUnique(sysAssociation)))
        {
            return error("修改协会'" + sysAssociation.getAssociationName() + "'失败，协会编码已存在");
        }
        sysAssociation.setUpdateBy(getUsername());
        return toAjax(sysAssociationService.updateSysAssociation(sysAssociation));
    }

    /**
     * 删除协会
     */
    @PreAuthorize("@ss.hasPermi('association:association:remove')")
    @Log(title = "协会", businessType = BusinessType.DELETE)
	@DeleteMapping("/{associationIds}")
    public AjaxResult remove(@PathVariable Long[] associationIds)
    {
        return toAjax(sysAssociationService.deleteSysAssociationByAssociationIds(associationIds));
    }

    /**
     * 获取协会会员列表
     */
    @GetMapping("/members/{associationId}")
    public AjaxResult getMembersByAssociationId(@PathVariable Long associationId)
    {
        List<SysAssociationMember> members = sysAssociationService.selectMembersByAssociationId(associationId);
        return success(members);
    }

    /**
     * 获取会员所属协会列表
     */
    @GetMapping("/memberAssociations/{membersId}")
    public AjaxResult getAssociationsByMembersId(@PathVariable Long membersId)
    {
        List<SysAssociationMember> associations = sysAssociationService.selectAssociationsByMembersId(membersId);
        return success(associations);
    }

    /**
     * 添加会员到协会
     */
    @PreAuthorize("@ss.hasPermi('association:association:edit')")
    @Log(title = "添加协会会员", businessType = BusinessType.INSERT)
    @PostMapping("/addMember")
    public AjaxResult addMember(@RequestBody SysAssociationMember sysAssociationMember)
    {
        return toAjax(sysAssociationService.addMemberToAssociation(sysAssociationMember));
    }

    /**
     * 从协会移除会员
     */
    @PreAuthorize("@ss.hasPermi('association:association:edit')")
    @Log(title = "移除协会会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeMember/{id}")
    public AjaxResult removeMember(@PathVariable Long id)
    {
        return toAjax(sysAssociationService.removeMemberFromAssociation(id));
    }

    /**
     * 更新协会会员数量
     */
    @PostMapping("/updateMemberCount/{associationId}")
    public AjaxResult updateMemberCount(@PathVariable Long associationId)
    {
        return toAjax(sysAssociationService.updateMemberCount(associationId));
    }
}
